package com.hmall.service;

import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.Crawler1Mapper;
import com.hmall.mapper.Crawler2Mapper;
import com.hmall.mapper.Crawler3Mapper;
import com.hmall.model.Crawler1;
import com.hmall.model.Crawler2;
import com.hmall.model.Crawler3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class CrawlerService {
    private final Crawler1Mapper crawler1Mapper;

    private final Crawler2Mapper crawler2Mapper;

    private final Crawler3Mapper crawler3Mapper;

    @Autowired
    public CrawlerService(Crawler1Mapper crawler1Mapper, Crawler2Mapper crawler2Mapper, Crawler3Mapper crawler3Mapper) {
        this.crawler1Mapper = crawler1Mapper;
        this.crawler2Mapper = crawler2Mapper;
        this.crawler3Mapper = crawler3Mapper;
    }


    public ServerResponse<Map<String, List<Crawler1>>> getCrawler1() {
        String [] names = crawler1Mapper.selectNames();
        Map<String, List<Crawler1>> map = new HashMap<>();
        for (String name : names){
            List<Crawler1> list = crawler1Mapper.selectByNameAndDate(name);
            map.put(name,list);
        }
        return ServerResponse.createBySuccess(map);
    }

    public ServerResponse getCrawler2() {
        String [] names = crawler2Mapper.selectNames();
        Map<String, List<Crawler2>> map = new HashMap<>();
        for (String name : names){
            List<Crawler2> list = crawler2Mapper.selectByNameAndDate(name);
            map.put(name,list);
        }
        return ServerResponse.createBySuccess(map);
    }

    public ServerResponse getCrawler3() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE,-1);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = dateFormat.format(calendar.getTime());
        List<Crawler3> list = crawler3Mapper.selectByDate(date);
        return ServerResponse.createBySuccess(list);
    }
}
